desc:Nym's note toggle/latch

in_pin:none
out_pin:none

slider1:0<0,16,1>Input MIDI Channel (0=Any)
slider2:0<0,1,1>REINIT

@init

NoteOFF = $x80;
NoteON = $x90;
note_array = 1000;
  memset(note_array, 0, 128);

@slider

channelIn = min(max(slider1 - 1 | 0, -1), 15); 
REINIT = slider2;

REINIT == 1 ? (
  memset(note_array, 0, 128);
  ii += 1;  
  REINIT = slider2 = 0;
);

@block

while ( 
  midirecv(offset, msg1, msg23) ? (
    status = msg1 & $xF0;
    channel = msg1 & $x0F;
    note = msg23 & $x7F;
    velocity = msg23 >> 8;  

    channel == channelIn || channelIn == -1 ? (
      status <= NoteON ? (
        status == NoteON && note_array[note] == 0 ? (
          midisend(offset, msg1, msg23);
          note_array[note] = 1;
        ):(
          status == NoteON && note_array[note] == 1 ? (
            midisend(offset, NoteOFF + channel, (note | 0 << 8));
            note_array[note] = 0;
          );
        );
      ):(  // Forward message if not a note event
        midisend(offset, msg1, msg23);  
      );  
    ):(  // Forward message if not on our channel  
      midisend(offset, msg1, msg23);
    );
  1;  // Force while loop to continue 
  );
);

